<?php

namespace app\admin\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class Category extends AdminBase
{
    /**
     * 查询配置树
     * @return Json
     */
    public function allTree(): Json
    {
        try {
            $tree=(new \app\common\business\Category())->getAllTree();
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($tree);
    }

    /**
     * 新增/保存数据
     * @return Json
     */
    public function save(): Json
    {
        list($id, $status,$sort,$pid,$name) = Param::postMore([
            ['id',null],
            ['status', 1],
            ['sort', 1],
            ['pid', 0],
            ['name',''],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
            'sort'=>$sort,
            'pid'=>$pid,
            'name'=>$name
        ];
        $validate = new \app\admin\validate\Category();
        $scene=empty($data['id'])?'create':'update';
        if (!$validate->scene($scene)->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new \app\common\business\Category())->save($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 修改状态
     * @return Json
     */
    public function updateStatus(): Json
    {
        list($id, $status) = Param::getMore([
            ['id',null],
            ['status', null],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
        ];
        try {
            $result = (new \app\common\business\Category())->updateStatus($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 修改状态
     * @return Json
     */
    public function updateMainStatus(): Json
    {
        list($id, $is_main) = Param::getMore([
            ['id',null],
            ['is_main', null],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'is_main'=>$is_main,
        ];
        try {
            $result = (new \app\common\business\Category())->updateMainStatus($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 根据id删除
     * @return Json
     */
    public function deleteById(): Json
    {
        $id=$this->request->get('id');
        try {
            $result=(new \app\common\business\Category())->deleteById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "删除成功");
        }
        return Show::error([], '删除失败');
    }

    /**
     * 根据分类名称查询子级配置
     * @return Json
     */
    public function category(): Json
    {
        $name=$this->request->get('name');
        try {
            $data=(new \app\common\business\Category())->getCategoryByName($name);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }
    /**
     * 根据分类名称查询子级配置 多个
     * @return Json
     */
    public function categoryMultiple(): Json
    {
        $name=$this->request->post('name');
        try {
            $data=(new \app\common\business\Category())->getCategoryMultipleByName($name);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }
}
